// pages/index/law_service/advisory/advisory.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: {},
    id: null,
    lawyer: null,
    //电话咨询 登记dialog
    phoneShow: false,
    //电话咨询  名字
    phoneName: '',
    //电话咨询  电话
    phonePhone: '',
    //电话咨询 拨打电话
    phoneCallShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      theme: app.globalData.theme,
      id: options.id
    })
    try {
      this.setData({
        phoneName: wx.getStorageSync("name"),
        phonePhone: wx.getStorageSync("phone")
      })
    }catch (e) {}
    this.getLawyerInfo()
  },

  /**
   * 获取律师详情
   */
  getLawyerInfo: function () {
    http.get("zz/people/detail/" + this.data.id,{
    }).then(res => {
      if (res.data.code === http.REQUEST_OK){
        this.setData({
          lawyer: res.data.data.people
        })
      }
    })
  },

  /**
   * 电话咨询
   * @param e
   */
  onPhoneAdvisory: function (e){
    if (this.data.phoneName.length <= 0 || this.data.phonePhone.length <= 0) {
      this.setData({
        phoneShow: true
      })
    }else {
      this.setData({
        phoneCallShow: true
      })
    }
  },
  /**
   * 电话咨询 姓名输入框实时监听
   * @param e
   */
  onInputPhoneName: function (e) {
    log.e(e.detail.value)
    this.setData({
      phoneName: e.detail.value
    })
  },
  /**
   * 电话咨询 联系电话输入框实时监听
   * @param e
   */
  onInputPhonePhone: function (e) {
    log.e(e.detail.value)
    this.setData({
      phonePhone: e.detail.value
    })
  },

  /**
   * 电话咨询 dialog 确定
   * @param e
   */
  onPhoneConfirm: function (e){
    if (this.data.phoneName.length <= 0) {
      Toast.fail("姓名不能为空")
      return false
    }
    if (this.data.phonePhone.length <= 0) {
      Toast.fail("联系电话不能为空")
      return false
    }
    if (this.data.phonePhone.length < 11) {
      Toast.fail("请输入正确的联系电话")
      return false
    }
    try {
      wx.setStorageSync("name",this.data.phoneName)
      wx.setStorageSync("phone",this.data.phonePhone)
    }catch (e){ }
    this.setData({
      phoneShow: false
    })
  },

  /**
   * 电话咨询 dialog 关闭
   * @param e
   */
  onPhoneClose: function (e){
    this.setData({
      phoneShow: false,
      phoneName: '',
      phonePhone: '',
    })
  },

  /**
   * 电话咨询 拨打电话
   * @param e
   */
  onPhoneCallConfirm: function (e){
    const _this = this
    if (_this.data.lawyer.phone == null || _this.data.lawyer.phone.length <= 0) {
      Dialog.alert({
        title: "温馨提示",
        message: "该律师没有联系电话，无法电话咨询"
      }).then(() => {  })
      return false
    }
    wx.makePhoneCall({
      phoneNumber: _this.data.lawyer.phone,
      success(res) {
        http.postJson("attorney_record/add",{
          type: 1,
          applet_user_id: app.globalData.userInfo.applet_user_id,
          user_name: _this.data.phoneName,
          zz_people_id: _this.data.id,
          area_code: app.globalData.areaCode,
          phone: _this.data.phonePhone
        }).then(res => { })
      }
    })
  },

  /**
   * 在线留言
   * @param e
   */
  onLeaveMessages: function (e){
    if (this.data.phoneName.length <= 0 || this.data.phonePhone.length <= 0) {
      this.setData({
        phoneShow: true
      })
    }else {
      wx.navigateTo({
        url: "../leave_messages/leave_messages?id=" + this.data.id
      })
    }
  },

  /**
   * 预约律师
   * @param e
   */
  onReserve: function (e) {
    if (this.data.phoneName.length <= 0 || this.data.phonePhone.length <= 0) {
      this.setData({
        phoneShow: true
      })
    }else {
      wx.navigateTo({
        url: "../reserve/reserve?id=" + this.data.id
      })
    }
  },

  /**
   * 我的预约
   * @param e
   */
  onMyReserve: function (e){
    if (this.data.phoneName.length <= 0 || this.data.phonePhone.length <= 0) {
      this.setData({
        phoneShow: true
      })
    }else {
      wx.navigateTo({
        url: "../my_reserve/my_reserve?id=" + this.data.id
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})